import $ from "jquery";
import './components/bgm/main';
import * as wordshow from './components/wordshow/main';
import * as fireworks from './components/fireworks/main';
import * as countdown from './components/countdown/main';
import * as ready from './components/ready/main';
import { daysCountdownStr } from './utils/date';
import { sleep, isFirtsFocus } from './utils/other';

const newYearTime = "2025-01-29 00:00:00.000"; // 倒计时目标时间
const countList = [
    countdown.num(),
    countdown.num(),
    countdown.num(),
    countdown.str("天"),
    countdown.num(),
    countdown.num(),
    countdown.str("时"),
    countdown.num(),
    countdown.num(),
    countdown.str("分"),
    countdown.num(),
    countdown.num(),
    countdown.str("秒"),
];

// 倒计时循环程序
setInterval(() => {
    var cds = daysCountdownStr(new Date(newYearTime));

    for (let i in countList) {
        if (countList[i].type == "num") countList[i].set(cds[i]);
    };
}, 1000);

function randomFireworks() { // 随机烟花
    fireworks.launch(Math.random() * $(window).width(), 300 + Math.random() * 400);
}

var readyFireworks;
function init() {
    fireworks.init();
    wordshow.init();
    if (new Date(newYearTime).valueOf() - Date.now() <= 0) {
        happyNewYear();
    } else if (new Date(newYearTime).valueOf() - Date.now() <= (10 * 1000)) {
        numberCountdown();
    } else {
        ready.init();

        readyFireworks = setInterval(() => {
            if (isVisiting()) randomFireworks();
        }, 1200);
    }
}

ready.onStart(async function () {
    clearInterval(readyFireworks);

    await sleep(1000);

    !async function () {
        const width = $(window).width();
        for (let x = 50; x < width - 50; x += width / 15) {
            if (isVisiting()) fireworks.launch(x, $(window).height() - (100 + Math.random() * 200));
            await sleep(1000);
        };
    }();
    await sleep(3000);

    for (let x of ["新的一年", "再接再厉", "再创辉煌"]) {
        let y = wordshow.leftRightFadeOutText(x).show();
        await sleep(2000);
        y.hide(true);
        await sleep(2000);
    };

    await sleep(2000);
    var x = wordshow.fadingText("现在").show();
    randomFireworks(); randomFireworks();
    await sleep(2000);
    x.hide(true);
    await sleep(2000);

    var x = wordshow.spaceFadingText("让我们").show();
    randomFireworks(); randomFireworks();
    await sleep(2000);
    x.hide(true);
    await sleep(2000);

    var x = wordshow.fadingText("迎接").show();
    randomFireworks(); randomFireworks();
    await sleep(2000);
    x.hide(true);
    await sleep(1000);

    var waitFireworks = setInterval(() => {
        if (isVisiting()) randomFireworks();
    }, 1000);

    ready.showNewYear();
    await sleep(2000);
    countdown.el.removeClass("hide");

    var lastloop = setInterval(() => {
        if (new Date(newYearTime).valueOf() - Date.now() <= (10 * 1000)) {
            clearInterval(waitFireworks);
            ready.hideNewYear();
            countdown.el.addClass("hide");
            sleep(500).then(() => numberCountdown());
            clearInterval(lastloop);
        }
    }, 100);

    var newYearLastLoop = setInterval(() => {
        if (new Date(newYearTime).valueOf() - Date.now() <= 0) {
            happyNewYear();
            clearInterval(newYearLastLoop);
        }
    }, 100);
});

function happyNewYear() {
    textFirewords("新年快乐", 100, $(window).width() / 2 - (100 * 2), $(window).height() - 200, 500, false);
    setInterval(() => {
        requestAnimationFrame(() => {
            if (isVisiting()) randomFireworks();
        });
    }, 650);
}

function numberCountdown() {
    textFirewords("987654321", 0, $(window).width() / 2, 500, 1000, true);
};

async function textFirewords(text, space, sx, up, st, autodel = false) {
    var x = sx;
    for (let tex of text) {
        let a = fireworks.launchWithText(x, up, tex, "#fff");
        x += space;
        await sleep(st);
        if (autodel) a.close();
    }
};

function isVisiting() {
    return document.visibilityState === "visible" && isFirtsFocus
}

init(); // 全局启动！！

// Service Worker 更新事件
if ('serviceWorker' in navigator) {
    navigator.serviceWorker.addEventListener('controllerchange', () => {
        window.location.reload();
    });
}